package com.sjsm.sjsmscenerecord.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.sjsm.sjsmscenerecord.ItemDetailActivity;
import com.sjsm.sjsmscenerecord.PhotoSettingActivity;
import com.sjsm.sjsmscenerecord.R;
import com.sjsm.sjsmscenerecord.adapter.MyProjectAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hasee on 2017/8/21.
 */

public class MyProjectFragment extends Fragment {
    @Bind(R.id.btn_new_project)
    Button btnNewProject;
    @Bind(R.id.lv_project)
    ListView lvProject;
    @Bind(R.id.btn_photo_setting)
    Button btnPhotoSetting;
    private int dataCount = 0;
    private List<Map<String, Object>> items;
    private MyProjectAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_project, container, false);
        ButterKnife.bind(this, view);
        items = new ArrayList<>();
        btnPhotoSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),PhotoSettingActivity.class));
            }
        });
        return view;

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.btn_new_project)
    public void onViewClicked() {
        dataCount++;
        Map<String, Object> item = new HashMap<>();
        item.put("name", getResources().getString(R.string.new_projecct_name) + dataCount);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        item.put("time", format.format(new Date(System.currentTimeMillis())));
        items.add(item);
        setData();
    }

    private void setData() {
        if (adapter == null) {
            adapter = new MyProjectAdapter(items, getActivity());
            lvProject.setAdapter(adapter);
            lvProject.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(), ItemDetailActivity.class);
                    intent.putExtra("name", items.get(position).get("name").toString());
                    intent.putExtra("time", items.get(position).get("time").toString());
                    startActivity(intent);
                }
            });
        } else {
            adapter.setData(items);
        }
    }
}
